package com.turing.manage.email.listener;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @desc   邮件发送定时任务类
 * @author wyh
 * @time   2022-01-07
 */
import java.util.TimerTask;

import javax.mail.MessagingException;

import com.turing.manage.email.user.UserService;
import com.turing.manage.email.utils.DateUtils;
import com.turing.manage.email.utils.EmailUtils;



public class EmailTask  extends TimerTask{

	UserService  userService=new UserService();
	
	
	
	@Override
	public void run()
	{
		 System.out.println("定时发送邮件。。。。。。。。");
		 try 
		 {
			List<Map<String, Object>> list_birthday = userService.queryBirthday();
			for(int i =0;i<list_birthday.size();i++)
			{
			Map<String, Object> map = list_birthday.get(i);
			String birthday = (String) map.get("birthday");	
			 Date now=new Date();
    	     SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd");
    	     int days = DateUtils.differentDaysByMillisecond(dFormat.format(now),birthday );
    	     System.out.println("i-----:"+(i)+"===days----:"+days);	
			
    	     System.out.println("i-----:"+(i)+"===days----:"+days);
    	     if (days>=0  && days<=5)
    	      {
    	    	 String email=(String) map.get("email");
    	    	 System.out.println("---birthday--->"+birthday);
    	    	 System.out.println("---email--->"+email);
    			 System.out.println("开发发送生日祝福的邮件。。。。。");
    	         String emailSubject="生日快乐";
				 String emailText="xxx企业全体员工给您送上生日祝福：祝愿您生日快乐";
				 String imageUrl="D:/birthday.gif";
				 EmailUtils.createMineMessage(email, emailSubject, emailText, imageUrl);
			}
    	     
    	     
			}
			
			
		} 
		 catch (ClassNotFoundException | SQLException | ParseException | UnsupportedEncodingException | MessagingException e) {
			System.out.println(e.getMessage());
		}
		
		
		
		
	}

}
